/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin.course;

import hibernate.dao.CenterDAO;
import hibernate.dao.CourseDAO;
import hibernate.dao.StaffDAO;
import hibernate.dao.StudentDAO;
import hibernate.entity.Centers;
import hibernate.entity.Courses;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ui.admin.AbstractAdminBean;
import ui.common.AbstractUIBean;

/**
 *
 * @author NDH
 */
@ManagedBean
@RequestScoped
public class CourseManager extends AbstractAdminBean {
    
    CourseDAO courseDAO = new CourseDAO();
    Centers center = new Centers();
    String name;
    int status = 0;
    Date startDate, endDate;
    Courses currCourse = new Courses();
    List<Courses> allCourse = new ArrayList<Courses>();
    List<Centers> allCenter;
    StudentDAO stuDao = new StudentDAO();
    StaffDAO staffDao = new StaffDAO();
    List<Courses> preList;

    public CourseManager() {
        isAuthor();
        center = new Centers();
        allCenter = new CenterDAO().readAll();
        allCourse = courseDAO.readLiving();
        preList = courseDAO.readPre();
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public List<Courses> getAllCourse() {
        return allCourse;
    }

    public Centers getCenter() {
        return center;
    }

    public void setCenter(Centers center) {
        this.center = center;
    }

    public List<Courses> getListC() {
        return allCourse;
    }

    public void setListC(List<Courses> listC) {
        this.allCourse = listC;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Courses getCurrCourse() {
        return currCourse;
    }

    public void setCurrCourse(Courses currCourse) {
        this.currCourse = currCourse;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * @param Courses a course to modify fields
     * @return String
     * @see A form with full of fields to modify
     */
    public String edit(Courses c) {
        name = c.getName();
        center = c.getCenters();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("editCourse", c);
        startDate = c.getStartDate();
        endDate = c.getEndDate();
        status = c.getStatus();
        return "edit";
    }

    /**
     * @return String
     * @see List of Course displayed
     */
    public String docreate() {
        currCourse = new Courses(center, name, startDate, endDate, status);
        if (courseDAO.create(currCourse) > 0) {
            showPrimeMessage("Success", "Course " + center.getName() + " created");
            allCourse = courseDAO.readLiving();
            return "list?faces-redirect=true";
        }
        return null;
    }

    /**
     * @param Courses a course for update its status
     * @return void
     */
    public void updateStatus(Courses c) {
        courseDAO.update(c);
    }

    /**
     * @return String
     * @see a list after some updated course displays
     */
    public String doupdate() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        Courses c = (Courses) session.getAttribute("editCourse");
        c.setStartDate(startDate);
        c.setEndDate(endDate);
        c.setName(name);
        c.setCenters(center);
        c.setStatus(status);
        if (courseDAO.update(c) > 0) {
            showPrimeMessage("Success", "Course " + center.getName() + " updated");
            return "list?faces-redirect=true";
        }
        showPrimeMessage("Error", "Unexpected error! update fail");
        return null;
    }

    /**
     * @param Courses a course for removing from the running system
     * @return String
     * @see a list of course after removing this course
     */
    public String remove(Courses c) {
        if (courseDAO.remove(c) > 0) {
            showPrimeMessage("Success", "Course " + center.getName() + " has been removed");
            return "list?faces-redirect=true";
        }
        return null;
    }

    /**
     * @param Courses a course chosen to pick staffs into
     * @return String
     * @see pickstaff page is displayed
     */
    public String pickStaff(Courses c) {
        HttpSession session = getHttpSession();
        session.removeAttribute("listS");
        session.removeAttribute("listR");
        session.setAttribute("pickCourse", c);
        return "pickstaff";
    }

    /**
     * @param Courses a course choosen to pick students into
     * @return String
     * @see pickstudent page is displayed
     */
    public String pickStudent(Courses c) {
        HttpSession session = getHttpSession();
        session.removeAttribute("listS");
        session.removeAttribute("listR");
        session.setAttribute("pickCourse", c);
        return "pickstudent";
    }

    /**
     * @return List<Centers>
     * @see a list of centers
     */
    public List<Centers> getAllCenter() {
        return allCenter;
    }
}
